package ymr.server;

import java.net.Socket;

import ymr.game.Game;
import ymr.game.Player;


enum Commands{
	INFO, ROLL, HELP, SCORE;
}

public class Protocol {

	private Commands command = Commands.HELP; // algne default
	private Player player;
	private Game game;
	private Socket clientSocket;

	public Protocol(Player p, Game g, Socket s){
		player = p;
		game = g;
		clientSocket = s;
	}

	/**
	 * @param test string which is searched among Commands.
	 * @return true, if Command exists, false if not.
	 */
	public boolean contains(String test) {
	    for (Commands c : Commands.values()) {
	        if (c.name().equalsIgnoreCase(test)) {
	            return true;
	        }
	    }
	    return false;
	}

	/**
	 * @param input string from a client.
	 * @return (string) what move player made or what was picked up or which commands are available
	 */
	public String processInput(String input) {
        String output = "";
        if(contains(input)){ // if there is such command (ilma igast muude parameetriteta, nt look)
        	command = Commands.valueOf(input.toUpperCase());
        }
        else{
        	output = "Unknown command>>>> " + input + "   ";
        	command = Commands.HELP;
        }

        switch(command){
        case INFO:
        	output += game.getInfo();
        	break;
        case ROLL:
        	if(player == game.getTurn()){
        		output += game.performAction();
        	}else{
        		output += "Please wait your turn.";
        	}
        	break;
        case HELP:
        	output += "Available commands: ";
        	output += "INFO - gives info about the location of every player. ";
        	output += "ROLL - rolls the die and moves accordingly. ";
        	output += "SCORE - shows your points. ";
        	break;
        case SCORE:
        	output = player + " score: " + Integer.toString(player.getPoints());
        	break;
        default:
        	output = "Unknown command: " + input;
        	break;

        }
        return output;
	}
}
